function convertDataURIToBinary (base64data) {
  let raw = window.atob(base64data)
  let rawLength = raw.length
  let array = new Uint8Array(new ArrayBuffer(rawLength))
  for (let i = 0; i < rawLength; i++) {
    array[i] = raw.charCodeAt(i) & 0xff
  }
  return array
}

function basetoBlob (base64data) {
  let u8arr = convertDataURIToBinary(base64data)
  let mine = 'application/pdf'
  return new Blob([u8arr], { type: mine })
}

function printFile (base64) {
  let iframe = document.getElementById('iframeFile')
  if (!iframe) {
    iframe = document.createElement('IFRAME')
  }
  let blob = basetoBlob(base64)
  let url = URL.createObjectURL(blob)
  iframe.setAttribute('src', url)
  iframe.setAttribute('id', 'iframeFile')
  iframe.setAttribute(
    'style',
    'position:absolute;width:0px;height:0px;left:-500px;top:-500px;'
  )
  document.body.appendChild(iframe)
  iframe.contentWindow.focus()
  iframe.contentWindow.print()
}

export default printFile